#index的views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.views.generic.base import RedirectView
from django.views.generic import ListView,DetailView,MonthArchiveView,WeekArchiveView
from django.views.generic.edit import CreateView,FormView,UpdateView, DeleteView
from .form import PersonInfoForm
from myapp.models import PersonInfo

def myapp(request):
    return render(request, 'myapp.html')

class MyListView(ListView):
    #设置模版文件
    template_name = 'MyListView.html'
    #设置模型外的数据
    extra_context = {'title':'人员信息表'}
    #查询模型外的数据
    queryset = PersonInfo.objects.all()
    #每页展示一条数据
    paginate_by =1
    #若不设置，则模版上下文默认为persioninfo_list
    #context_object_name='personinfo'

class MyDetailView(DetailView):
    #设置模版文件
    template_name = 'MyDetailView.html'
    #设置模型外的数据
    extra_context = {'title':'人员信息表'}
    #设置模型的查询字段
    slug_field ='age'
    #设置路由的变量名，与属性slug_field实现模型的查询操作
    slug_url_kwarg='age'
    pk_url_kwarg = 'pk'
    #设置查询模型PersonInfo
    model = PersonInfo
    #属性queryset可以做简单的查询操作
    #queryset=PersonInfo.objects.all()
    #若不设置，则模版上下文默认为persioninfo
    #context_object_name='personinfo'
    #是否将pk和slug作为查询条件
    #query_pk_and_slug=False

def result(request):
    return HttpResponse('Success')

class MyFormView(FormView):
    initial={'name':'Betty','age':30,'hiredate':'2020-01-01'}
    template_name = 'MyFormView.html'
    success_url = '/result'
    form_class = PersonInfoForm
    extra_context = {'title':'人员信息表'}

class MyCreateView(CreateView):
    initial={'name':'Betty','age':30,'hiredate':'2020-01-01'}
    template_name = 'MyFormView.html'
    success_url = '/result'
    #表单生成方式一
    #form_class = PersonInfoForm
    #表单生成方式二
    model=PersonInfo
    #fields设置模型字段，从而生成表单字段
    fields = ['name','age','hiredate']
    extra_context = {'title':'人员信息表'}

class MyUpdateView(UpdateView):
    template_name = 'MyUpdateView.html'
    success_url = '/result'
    model = PersonInfo
    #fields设置模型字段，从而生成表单字段
    fields = ['name','age','hiredate']
    slug_url_kwarg = 'pk'
    slug_field = 'pk'
    context_object_name = 'personinfo'
    extra_context = {'title':'人员信息表'}


class MyDeleteView(DeleteView):
    template_name = 'MyDeleteView.html'
    success_url = '/result'
    model = PersonInfo
    context_object_name = 'personinfo'
    extra_context = {'title': '人员信息表'}


class MyMonthArchiveView(MonthArchiveView):
    allow_empty = True
    allow_future = True
    context_object_name = 'mylist'
    template_name = 'MyMonthArchiveView.html'
    success_url = '/result'
    model = PersonInfo
    date_field = 'hiredate'
    queryset = PersonInfo.objects.all()
    year_format = '%Y'
    #month_format默认值为%b，支持英文日期，如Oct
    month_format = '%m'
    paginate_by = 50


class MyWeekArchiveView(WeekArchiveView):
    allow_empty = True
    allow_future = True
    context_object_name = 'mylist'
    template_name = 'MyWeekArchiveView.html'
    model = PersonInfo
    date_field = 'hiredate'
    queryset = PersonInfo.objects.all()
    year_format = '%Y'
    week_format = '%W'
    paginate_by = 50